﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {

    public enum BacklogFilter {
        FULL_BACKLOG,
        SELECTED_BACKLOG,
        PLANNED_BACKLOG
    }
    
    
    /// <summary>
    /// Interaction logic for BacklogPanel.xaml
    /// </summary>
    public partial class BacklogPanel : GenericPanel {


        /// <summary>
        /// Default Constructor.
        /// </summary>
        public BacklogPanel() {
            InitializeComponent();
            
        }

        


        #region DataContext Methods

        /// <summary>
        /// Request external data besides the projectdataset.
        /// There is no data to request, but update the datagridview.
        /// THIS SHOULD BE DONE HERE. and NOT at the ContextChange in order to
        /// preserve the datagrid columns width.
        /// </summary>
        /// <returns></returns>
        protected override bool RequestData() {
            backlogDataGrid.UpdateDataGridViewCollection();
            return true;
        }

        #endregion


        #region Button Actions

     

        

        /// <summary>
        /// Adds a new sprint.
        /// </summary>
        private void AddSprint_Executed(object sender, ExecutedRoutedEventArgs e) {
            AddNewSprint();
        }

        private void PlanSprint_Executed(object sender, ExecutedRoutedEventArgs e) {
            PlanSprint();
        }

      

        /// <summary>
        /// When clicked exports the backlog to a Microsoft Project xml file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exportProjectButton_Click(object sender, RoutedEventArgs e) {                    
            ExportBacklogToMSProject(DataContextAsProjectDataSet);
        }

        #endregion


        /// <summary>
        /// Exports the dataset to a Microsoft Project xml file.
        /// </summary>
        /// <param name="project"></param>
        private void ExportBacklogToMSProject(Data.ProjectDataSet project) {            

            System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
            saveDialog.FileName = "TimeLine - " + project.Project[0].ClientName + "-" + project.Project[0].ProjectName + ".xml";
            saveDialog.Filter = "MS Project XML file|*.xml";

            System.Windows.Forms.DialogResult d = saveDialog.ShowDialog();
            if (d == System.Windows.Forms.DialogResult.Cancel)
                return;

            string xml = null;
            try {
                xml = Client.Service.GenerateBackLogReportXAML(project.Project[0].ProjectUId, "_MSProjectExport.xslt");
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, "Sorry, could not export to Microsoft Project.");
                return;
            }

            System.IO.StreamWriter sw = new System.IO.StreamWriter(saveDialog.FileName);
            sw.Write(xml);
            sw.Close();
        }

      
        

        private void AddNewSprint() {
            Data.ProjectDataSet dsSprintItems = new BadHabit.Factory.Data.ProjectDataSet();
            dsSprintItems.EnforceConstraints = false;
            try {
                dsSprintItems.Merge(Client.Service.RetriveSprintDefaultItems(Project.Category));
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, "Sorry, could not retrive sprint default items");
                return;
            }

            Project.AddNewSprint(dsSprintItems);
                

            backlogDataGrid.UpdateDataGridViewCollection();
        }

       

        private void PlanSprint_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (Project == null)
                e.CanExecute = false;
            else
                e.CanExecute = true;
            
            //e.CanExecute = Project.CanCreateANewBaseLine;
        }

        

        private void PlanSprint() {

            MessageBoxResult d;

            // if there are no more Sprint to plan, add a new one? before plan
            if (!Project.CanCreateANewBaseLine) {
                d = Windows.Error.ShowDialog(
                String.Format(Properties.Resources.All_Sprints_are_over_Add_new_Sprint, Project.NextBaseLine),
                Properties.Resources.Plan, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                if (d == MessageBoxResult.No)
                    return;

                AddNewSprint();

            } else {

                d = Windows.Error.ShowDialog(
                String.Format(Properties.Resources.You_are_about_to_start_planning_sprint_N, Project.NextBaseLine),
                Properties.Resources.Plan, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                if (d == MessageBoxResult.No)
                    return;
            }
                        
            Project.CreateNewBaseLine();
            
            backlogDataGrid.UpdateDataGridViewCollection();

           
        }

        private void backlogPanel_Loaded(object sender, RoutedEventArgs e) {
            backlogDataGrid.UpdateDataGridViewCollection();
            backlogDataGrid.backlogDataGrid.Focus();
        }

        private void autoPlanButton_Click(object sender, RoutedEventArgs e) {
            if (autoPlanButton.IsChecked==true) {
                Windows.Error.ShowAlert(autoPlanButton.ToolTip.ToString());
            }
        }

        private void backlogPanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
            //DataContextAsProjectDataSet.BacklogItem.ColumnChanged += new System.Data.DataColumnChangeEventHandler(BacklogItem_ColumnChanged);
            //DataContextAsProjectDataSet.BacklogItemHours.ColumnChanged += new System.Data.DataColumnChangeEventHandler(BacklogItemHours_ColumnChanged);
        }

        private void addItemButton_Click(object sender, RoutedEventArgs e) {            
            backlogDataGrid.AddNewBacklogItem();

        }

        //void BacklogItemHours_ColumnChanged(object sender, System.Data.DataColumnChangeEventArgs e) {
        //    ShowBacklogLatePlanAlert();
        //}

        //void BacklogItem_ColumnChanged(object sender, System.Data.DataColumnChangeEventArgs e) {
        //    ShowBacklogLatePlanAlert();
        //}
        
        //private void ShowBacklogLatePlanAlert() {            
        //    if (DataContextAsProjectDataSet.Project.Count == 0)
        //        return;
        //    if (DataContextAsProjectDataSet.Project[0].IsReplanning)
        //        return;
        //    if ((bool)DataContextAsProjectDataSet.Project[0]["IsPlanningLate"])
        //        Windows.Error.ShowAlert("Your sprint plan is late.\nYou should re-plan the sprint before make changes at your backlog items.");
        //}




    }
}
